﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELab.Client;
using ELab.Message;
using ELab.QuestionAnwer;
using ELab.Serialization.Data;
using ELab.Communication.Messages;
using ELab.View.TestExample;
using ELab.Management;
using ELab.Settings;
using ELab.Communication;
using ELab.Communication.Messages.ControllerMessages;

namespace ELab.Student.Controls
{
    public partial class ctrlTableMode : UserControl
    {
        public ELabClient _student;
        public LessonTest examp;
        public int timeRemaining = 0;
        public int index = 0;
        private List<QuestionResult> results;
        private bool isSent = false;
        public User CurrentUser = null;
        public event EventHandler RaiseComplete;
        public IEnumerable<User> Users { get; set; }
        public ClassMode viewMode;

        public ctrlTableMode()
        {
            InitializeComponent();
            QADataGridView.AllowUserToAddRows = false;
            QADataGridView.AutoGenerateColumns = false;
            QADataGridView.CellMouseClick += QADataGridView_CellMouseClick;
        }

        void QADataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            QADataGridView.BeginEdit(false);
        }

        public void LoadQuestionForm()
        {
            // clear panel
            QADataGridView.Columns.Clear();
            QADataGridView.Rows.Clear();
            QADataGridView.Refresh();
            // Initialize result
            if (results == null)
                results = examp.Questions.Select(c => new QuestionResult() { Question = c, Result = string.Empty }).ToList();

            // set issent result is false
            isSent = false;

            for (int i = 0; i < examp.Questions.Count; i++)
            {
                QADataGridView.Columns.Add(string.Format("Question{0}", i + 1), examp.Questions[i].QuestionValue);
            }

            for (int i = 0; i < examp.Questions[0].Answers.Length; i++)
            {
                DataGridViewRow row = new DataGridViewRow();
                for (int j = 0; j < examp.Questions.Count; j++)
                {
                    DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                    cell.Value = examp.Questions[j].Answers[i].AnswerValue;
                    row.Cells.Add(cell);
                }
                QADataGridView.Rows.Add(row);
            }
            QADataGridView.Refresh();
        }

        public void SentResult()
        {
            RaiseComplete(this, null);

            TestExampleResult testresult = new TestExampleResult()
            {
                Results = results,
                Student = CurrentUser.Fullname
            };
            var dataArr = testresult.SerializeToByteArray();
            CommandMessage comm = new CommandMessage { CommandType = ELab.Message.CommandType.RequestTableExampleResult, Body = dataArr };
            EMessage mess = new EMessage { Command = comm };

            var ms = _student.CreateMessage();
            ms.DestinationApplicationName = this.Users.FirstOrDefault(u => u.Type == UserType.Teacher).UserName;
            ms.MessageData = mess.Data;
            ms.TransmitRule = MessageTransmitRules.NonPersistent;
            ms.Send();

            results = results = examp.Questions.Select(c => new QuestionResult() { Question = c, Result = string.Empty }).ToList();
        }
        private void GetCurrentResult()
        {
            if (results != null && QADataGridView.Rows.Count > 0)
            {
                for (int i = 0; i < QADataGridView.Rows.Count; i++)
                {
                    DataGridViewRow row = QADataGridView.Rows[i];
                    for (int j = 0; j < results.Count; j++)
                    {
                        results[j].Question.Answers[i].AnswerValue = (string)row.Cells[j].Value;
                    }
                }
            }
        }
        private void btnFinished_Click(object sender, EventArgs e)
        {
            FinishTesting();
        }

        public void FinishTesting()
        {
            GetCurrentResult();
            SentResult();
        }
    }
}
